/*
 *                    COPYRIGHT NOTICE
 *
 *           This code is copyright protected by Freedom Inc.
 *
 *              2001-2012 All Rights Are Reserved
 */
package com.freedom.oldsong.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.freedom.oldsong.common.AppConstants;
import com.freedom.oldsong.system.dto.MenuDto;
import com.freedom.oldsong.system.model.User;
import com.freedom.oldsong.system.service.MenuHolder;
import com.freedom.oldsong.system.service.UserHolder;
import com.freedom.oldsong.util.CommonWebContextUtil;
import com.freedom.oldsong.util.FormatUtils;
import com.freedom.oldsong.util.LanguageUtil;

/**
 * @author steven.wang
 *
 */
public abstract class BaseAction {
	
	private Long orgId = null;

	private User user = null;
	
	protected List<String> errors = new ArrayList<String>();
	
	protected Map<String,Object> jasonMap = new HashMap<String, Object>();

	public abstract String getSystemType();

	public List<MenuDto> getMenus() {
		ServletContext sc = ServletActionContext.getServletContext();
		ApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(sc);
		MenuHolder menuHolder=(MenuHolder)ctx.getBean("menuHolder");
	    return menuHolder.getMenus();
	}

	/**
	 * Get current user
	 * 
	 * @return current user
	 */
	public User getUser() {
		if (user == null) {
			user = (User) CommonWebContextUtil.getFromSession(AppConstants.USER_INFO);
			if (user == null) {
				UserHolder userHolder = (UserHolder) CommonWebContextUtil.getContextBean("userHolder");
				user = userHolder.getCurrentUser();
				CommonWebContextUtil.saveInSession(AppConstants.USER_INFO,user);
				CommonWebContextUtil.saveInSession(LanguageUtil.LANGUAGE,user.getShowLanguage());
			}
		}
		return user;
	}

	public String getLanguage() {
		return getUser().getShowLanguage();
	}

	/**
	 * Get current login name.
	 * 
	 * @return current login name
	 */
	public String getLoginName() {
		if (user != null) {
			return user.getLoginName();
		} else {
			return CommonWebContextUtil.getLoginName();
		}
	}
	
	public String getFormatedUserName() {
		String loginName = this.getLoginName();
		if (loginName != null && loginName.indexOf('@') >= 0) {
			User u = getUser();
			if(u != null){
				return u.getUsername();
			}
		}
		return FormatUtils.formatMemberNames(loginName, ".");
	}
	
	public HttpServletRequest getRequest(){
		return (HttpServletRequest)ServletActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
	}
	
	public void saveInRequest(String name,Object value){
		CommonWebContextUtil.saveInRequest(name, value);
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}
}
